package transaction.mq.order.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import transaction.mq.entity.Result;
import transaction.mq.order.config.RabbitMQConfig;
import transaction.mq.order.mapper.XcOrdersDetailMapper;
import transaction.mq.order.mapper.XcOrdersMapper;
import transaction.mq.order.mapper.XcTaskMapper;
import transaction.mq.order.model.XcOrders;
import transaction.mq.order.model.XcOrdersDetail;
import transaction.mq.order.model.XcTask;
import transaction.mq.util.IdWorker;

import java.util.*;

@Service
public class XcOrderService {

    private IdWorker idWorker = new IdWorker(0, 0);

    @Autowired
    private XcOrdersMapper xcOrdersMapper;
    @Autowired
    private XcOrdersDetailMapper xcOrdersDetailMapper;
    @Autowired
    private XcTaskMapper xcTaskMapper;

    public List<XcOrders> getAllOrders() {
        return xcOrdersMapper.selectAll();
    }

    /**
     * 创建订单
     *
     * @return
     */
    public Result create() {
        long orderId = idWorker.nextId();
        long orderDetailId = idWorker.nextId();
        long taskId = idWorker.nextId();
        XcOrders orders = createOrder(orderId);
        XcOrdersDetail orderDetails = createOrderDetails(orderDetailId);
        orderDetails.setOrderNumber(orderId + "");
        orders.setDetails(JSONObject.toJSONString(orderDetails));
        xcOrdersMapper.insertSelective(orders);
        xcOrdersDetailMapper.insertSelective(orderDetails);
        // 保存消息记录到task
        XcTask xcTask = createTask(taskId + "", orders.getUserId(), orderDetails.getItemId());
        xcTaskMapper.insertSelective(xcTask);
        return new Result();
    }

    private XcTask createTask(String id, String userId, String courseId) {
        XcTask xcTask = new XcTask();
        xcTask.setCreateTime(new Date());
        xcTask.setUpdateTime(new Date());
        xcTask.setId(id);
        xcTask.setVersion(0);
        xcTask.setMqRoutingkey(RabbitMQConfig.XC_LEARNING_ADDCHOOSECOURSE_KEY);
        xcTask.setMqExchange(RabbitMQConfig.EX_LEARNING_ADDCHOOSECOURSE);
        //{"userId":"49","courseId":"4028e581617f945f01617f9dabc40000"}
        Map map = new HashMap<>(4);
        map.put("userId", userId);
        map.put("courseId", courseId);
        xcTask.setRequestBody(JSONObject.toJSONString(map));
        return xcTask;
    }

    private XcOrders createOrder(long nextId) {
        XcOrders xcOrders = new XcOrders();
        xcOrders.setStartTime(new Date());
        xcOrders.setEndTime(new Date());
        xcOrders.setOrderNumber(nextId + "");
        xcOrders.setInitialPrice(0.01f);
        xcOrders.setPrice(0.01f);
        xcOrders.setPrice(0.01f);
        xcOrders.setStatus(401001 + "");
        xcOrders.setUserId("49");
        return xcOrders;
    }

    private XcOrdersDetail createOrderDetails(long nextId) {
        XcOrdersDetail xcOrdersDetail = new XcOrdersDetail();
        xcOrdersDetail.setStartTime(new Date());
        xcOrdersDetail.setEndTime(new Date());
        xcOrdersDetail.setValid("1");
        xcOrdersDetail.setId(nextId + "");
        xcOrdersDetail.setItemId(idWorker.nextId() + "");
        xcOrdersDetail.setItemNum(1);
        xcOrdersDetail.setItemPrice(0.01f);
        return xcOrdersDetail;
//        [{"itemId":"402885816243d2dd016243f24c030002","itemNum":1,"itemPrice":0.01,"orderNumber":"318344201396162560"}]
    }
}
